function [] = DYNA2OptiStruct(DYNAin,optiStructOut)
% convert DYNA files to OptiStruct format for hypermesh

if isempty(strfind(DYNAin,'.k'))
    DYNAin = [DYNAin '.k']; 
end

if isempty(strfind(optiStructOut,'.fem'))
    optiStructOut = [optiStructOut '.fem']; 
end

%beam elements not supported for optistruct yet
disp('Reading DYNA file...');
[nodes,DYNAsolids,DYNAshells,~] = getElemNodes(DYNAin);
disp('Finished Reading DYNA File.')
fid = fopen(optiStructOut,'w');

disp('Writing nodes to OptiStruct file...');
if ~isempty(nodes)
    for i = 1:size(nodes,1)
        fprintf(fid,'GRID    %8s        %8s%8s%8s\n',...
            num2fixedlengthstr(nodes(i,1),8,1,0),...
            num2fixedlengthstr(nodes(i,2),8,1,0),...
            num2fixedlengthstr(nodes(i,3),8,1,0),...
            num2fixedlengthstr(nodes(i,4),8,1,0));
    end
end
disp('Finished writing nodes to OptiStruct file.');

disp('Writing solid elements to OptiStruct file...');
if ~isempty(DYNAsolids)
    for i = 1:size(DYNAsolids,1)
        if DYNAsolids(i,6) == DYNAsolids(i,7) %solid is a tet
            fprintf(fid,'CTETRA  %8s%8s%8s%8s%8s%8s\n',...
                num2fixedlengthstr(DYNAsolids(i,1),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,2),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,3),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,4),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,5),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,6),8,1,0));
        else %solid is a hex
            fprintf(fid,'CHEXA   %8s%8s%8s%8s%8s%8s%8s%8s\n+       %8s%8s\n',...
                num2fixedlengthstr(DYNAsolids(i,1),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,2),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,3),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,4),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,5),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,6),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,7),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,8),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,9),8,1,0),...
                num2fixedlengthstr(DYNAsolids(i,10),8,1,0));            
        end
    end
end
disp('Finished writing solid elements to OptiStruct file.');

disp('Writing shell elements to OptiStruct file...');
if ~isempty(DYNAshells)
    for i = 1:size(DYNAshells,1)
        if DYNAshells(i,5) == DYNAshells(i,6) %shell is a tri
            fprintf(fid,'CTRIA3  %8s%8s%8s%8s%8s\n',...
                num2fixedlengthstr(DYNAshells(i,1),8,1,0),...
                num2fixedlengthstr(DYNAshells(i,2),8,1,0),...
                num2fixedlengthstr(DYNAshells(i,3),8,1,0),...
                num2fixedlengthstr(DYNAshells(i,4),8,1,0),...
                num2fixedlengthstr(DYNAshells(i,5),8,1,0));
        else %shell is a quad
            fprintf(fid,'CQUAD4  %8s%8s%8s%8s%8s%8s\n',...
                num2fixedlengthstr(DYNAshells(i,1),8,1,0),...
                num2fixedlengthstr(DYNAshells(i,2),8,1,0),...
                num2fixedlengthstr(DYNAshells(i,3),8,1,0),...
                num2fixedlengthstr(DYNAshells(i,4),8,1,0),...
                num2fixedlengthstr(DYNAshells(i,5),8,1,0),...
                num2fixedlengthstr(DYNAshells(i,6),8,1,0));            
        end
    end
end
disp('Finished writing shell elements to OptiStruct file.');

disp('DYNA to OptiStruct file conversion complete.');
fclose(fid);

% end DYNA2OptiStruct.m
end